package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvReturnInfo;
@Service
public class InvReturnInfoDAO extends BaseDAO<InvReturnInfo, Integer>{
	/**
	 * 根据机构编号查询零售单信息
	 * @param oc 机构编号
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public RESULT findByMap(Byte type,List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(null == ocs || ocs.size() == 0) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvReturnInfo s WHERE s.type = ? AND s.oc IN (");
		List<Object> list=new ArrayList<Object>();
		list.add(type);
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String cn = map.get(Parm.CN); 
			String dn = map.get(Parm.DN);	
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.deleted = ?");
			}
			if(isNotNull(time1)){
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND s.ct >= ?");
			}
			if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND s.ct <= ?");
			}
			if(isNotNull(cn)){
				list.add("%" + cn + "%");
				hql.append(" AND s.cu IN (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
			}
			if(isNotNull(dn)){
				list.add("%" + dn + "%");
				hql.append(" AND s.dn LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
}
